﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DFControlLibrary
{
    public delegate void TimeEndEventHandler(object sender, EventArgs e);

    public partial class CountDownTimer : Label
    {

        /* 内部円領域の幅 */ 
        private int innerWidth = 80;
        /* 内部円領域の高さ */ 
        private int innerHeight = 80;
        /* カウントダウン時間（秒） */ 
        private int countTime = 60;
        /* 残る時間 */
        private int remainTime = 60;
        /* 60秒以上塗つぶし領域の色 */
        private Color fillColor = Color.CornflowerBlue;
        /* 60秒以下塗つぶし領域の色 */
        private Color lightFillColor = Color.LightBlue;
        /* 塗つぶし領域の背景色 */
        private Color backFillColor = SystemColors.InactiveCaption;
        private SolidBrush fillBrush = new SolidBrush(Color.CornflowerBlue);
        private SolidBrush lightFillBrush = new SolidBrush(Color.LightBlue);
        private SolidBrush backFillBrush = new SolidBrush(SystemColors.InactiveCaption);

        public event TimeEndEventHandler TimeEnd;

        [Description("内部領域の幅"), Category("Layout")]
        public int InnerWidth
        {
            get
            {
                return innerWidth;
            }
            set
            {
                if (value > Width)
                    innerWidth = 80;
                else
                    innerWidth = value;
            }
        }
        [Description("内部領域の高さ"), Category("Layout")]
        public int InnerHeight
        {
            get
            {
                return innerHeight;
            }
            set
            {
                if (value > Height)
                    innerHeight = 80;
                else
                    innerHeight = value;
            }
        }
        [Description("カウントダウン時間（秒）"), Category("Data")]
        public int CountTime
        {
            get
            {
                return countTime;
            }
            set
            {
                if (value >= 6000)
                    countTime = 5999;
                else if (value < 0)
                    countTime = 0;
                else
                    countTime = value;
                remainTime = countTime;
                this.Text = string.Format("{0:D2}分\n{1:D2}秒", remainTime / 60, remainTime % 60);
                Invalidate();
            }
        }
        [Description("60秒以上塗つぶし領域の色"), Category("Appearance")]
        public Color FillColor
        {
            get
            {
                return fillColor;
            }
            set
            {
                fillColor = value;
                fillBrush = new SolidBrush(fillColor);
            }
        }
        [Description("60秒以下塗つぶし領域の色"), Category("Appearance")]
        public Color LightFillColor
        {
            get
            {
                return lightFillColor;
            }
            set
            {
                lightFillColor = value;
                lightFillBrush = new SolidBrush(lightFillColor);
            }
        }
        [Description("塗つぶし領域の背景色"), Category("Appearance")]
        public Color BackFillColor
        {
            get {    return backFillColor;            }
            set {
                backFillColor = value;
                backFillBrush = new SolidBrush(backFillColor);
            }
        }

        public bool IsStartCount
        {
            get { return timer1.Enabled; }
        }

        public CountDownTimer()
        {
            InitializeComponent();
            timer1.Interval = 1000;
            remainTime = countTime;
            this.AutoSize = false;

            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddEllipse(0, 0, Width, Height);
            this.Region = new Region(path);
        }

        ~CountDownTimer()
        {
            TimerStop();
        }

        /// <summary>
        /// タイマ開始
        /// </summary>
        public void TimerStart()
        {
            timer1.Start();            
        }

        /// <summary>
        /// タイマ停止
        /// </summary>
        public void TimerStop()
        {
             timer1.Stop();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Rectangle innerRect = new Rectangle((Width - innerWidth) / 2, (Height - innerHeight) / 2,
                    innerWidth, innerHeight);
            if (remainTime >= 60)
            {
                int angle = (60 - remainTime % 60) * 6;
                g.FillRegion(lightFillBrush, this.Region);
                g.FillPie(fillBrush, 0, 0, Width, Height, -90 + angle, 360 - angle);
                g.FillEllipse(Brushes.LightGray, innerRect);
            }
            else
            {
                int angle;
                if (0 < countTime)
                {
                    if (countTime < 60)
                    {
                        angle = (countTime - remainTime) * 360 / countTime;
                    }
                    else
                    {
                        angle = (60 - remainTime) * 6;
                    }
                    g.FillRegion(backFillBrush, this.Region);
                    g.FillPie(lightFillBrush, 0, 0, Width, Height, -90 + angle, 360 - angle);
                    g.FillEllipse(Brushes.LightGray, innerRect);
                }
            }

            string[] str = this.Text.Split('\n');
            if (str.Length == 2)
            {
                innerRect.Inflate(-3, -10);
                innerRect.Y += 10;
                StringFormat format = new StringFormat();
                format.LineAlignment = StringAlignment.Near;
                format.Alignment = StringAlignment.Center;
                g.DrawString(str[0], Font, new SolidBrush(this.ForeColor), innerRect, format);

                format.LineAlignment = StringAlignment.Center;
                Font font = new Font(this.Font.FontFamily, this.Font.Size + 10);
                g.DrawString(str[1], font, new SolidBrush(this.ForeColor), innerRect, format);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            remainTime--;
            this.Text = string.Format("{0:D2}分\n{1:D2}秒", remainTime / 60, remainTime % 60);
            Invalidate();
 
            if (remainTime < 0)
            {
                timer1.Stop();
                EventArgs ea = new EventArgs();
                TimeEnd(this, ea);
            }
        }

        private void CountDownTimer_Resize(object sender, EventArgs e)
        {
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddEllipse(0, 0, Width, Height);
            this.Region = new Region(path);
        }
    }
}
